package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Coupon;
import com.cskaoyan.bean.CouponMylistResult;
import com.cskaoyan.bean.CouponResponseVo;
import com.cskaoyan.bean.wx.CouponPart;
import com.cskaoyan.mapper.CouponUserMapper;
import com.cskaoyan.service.wx.WxCouponService;
import com.cskaoyan.service.wx.WxCouponUserService;
import com.cskaoyan.utils.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @author WeitenHong
 * @date 2020/10/8 11:05
 */

@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {
    @Autowired
    WxCouponService wxCouponService;

    @Autowired
    WxCouponUserService couponUserService;



    //显示我所拥有的优惠券
    @RequestMapping("mylist")
    public BaseRespVo myCouponList(Integer status, Integer page, Integer size) {
        Integer userId = IdUtils.getId();
        HashMap<String, Object> map = wxCouponService.showMyCouponList(status, page, size, userId);
        return BaseRespVo.ok(map);
    }

    //添加优惠券
    @RequestMapping("receive")
    public CouponRespVo receive(@RequestBody CouponPart couponPart) {
        if (IdUtils.getId() == 0) {
            CouponRespVo baseRespVo = new CouponRespVo();
            baseRespVo.setErrno(501);
            baseRespVo.setErrmsg("请登录");
            return baseRespVo;
        }
        Integer userId = IdUtils.getId();
        CouponRespVo couponRespVo=couponUserService.receive(userId,couponPart.getCouponId());
        return couponRespVo;
    }

    //选择优惠券
    @RequestMapping("selectlist")
    public BaseRespVo selectCouponInOrder(Integer cartId, Integer grouponRulesId){
        List<CouponMylistResult> couponList=wxCouponService.selectCoupon(cartId,grouponRulesId);
        return BaseRespVo.ok(couponList);

    }

    //用优惠码兑换优惠券
    @PostMapping("exchange")
    public CouponRespVo exchangeCoupon(@RequestBody Coupon coupon){
        Integer userId = IdUtils.getId();
        String code = coupon.getCode();
        CouponRespVo couponResp=wxCouponService.exchangeCouponByCode(code,userId);
        return couponResp;
    }



}
